package com.white.commonlib.utils;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.SystemClock;
import android.provider.Settings;
import android.support.annotation.DrawableRes;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.white.commonlib.R;
import com.white.commonlib.base.AppContext;

import java.lang.reflect.Method;

/**
 * Created by Administrator on 2017/5/9 0009.
 */

public class PhoneUtils
{
//    public static String getIMEI(Context context)
//    {
//        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
//        if (tm != null)
//        {
//            return tm.getDeviceId();
//        }
//        return "NO_IMWEI";
//    }

    public static String getBluetoothMac()
    {
        BluetoothAdapter m_BluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        String str = null;
        if (m_BluetoothAdapter != null)
        {
            str = m_BluetoothAdapter.getAddress();
        } else
        {
            str = "NO_BLUETOOTH";
        }
        if(TextUtils.isEmpty(str))
        {
            return "NO_BLUETOOTH";
        }else
        {
            return str;
        }
    }

    public static String getPhoneId(Context ctx)
    {
        String m_szAndroidID = Settings.Secure.getString(ctx.getContentResolver(), Settings.Secure.ANDROID_ID);
        if (TextUtils.isEmpty(m_szAndroidID))
        {
            return "NO_PHONE_ID";
        } else
        {
            return m_szAndroidID;
        }
    }

    public static String getDeviceSN()
    {
        String var0 = null;
        try
        {
            Class var1 = Class.forName("android.os.SystemProperties");
            Method var2 = var1.getMethod("get", new Class[]{String.class, String.class});
            var0 = (String) ((String) var2.invoke(var1, new Object[]{"ro.serialno", "unknown"}));
        } catch (Exception var3)
        {
        }
        if(TextUtils.isEmpty(var0))
        {
            var0 = "NO_SERILNUM";
        }
        return var0;
    }

    public static String getPhoneUniqueNum(Context ctx)
    {
        String bs = getBluetoothMac();
        String as = getPhoneId(ctx);
        String ss = getDeviceSN();
        return bs + as + ss;
    }

    /**
     * 获取versioncode
     *
     * @return
     */
    public static int getVersionCode(Context mContext)
    {
        PackageManager pm = mContext.getPackageManager();
        try
        {
            PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_CONFIGURATIONS);
            return pi.versionCode;
        } catch (PackageManager.NameNotFoundException e)
        {
            e.printStackTrace();
        }
        return 0;
    }

    /***
     * 获取版本名
     * @param mContext
     * @return
     */
    public static String getVersionName(Context mContext)
    {
        PackageManager pm = mContext.getPackageManager();
        try
        {
            PackageInfo pi = pm.getPackageInfo(mContext.getPackageName(), PackageManager.GET_CONFIGURATIONS);
            return pi.versionName;
        } catch (PackageManager.NameNotFoundException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public static void backgroundAlpha(Activity context, float bgAlpha)
    {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

    public static void toast(Context activity, String msg)
    {
        toast(activity, msg, -1);
    }

    private static long lastToastTime;
    private static int toastHeightOffset;
    public static void toast(Context activity, String msg, @DrawableRes int iconId)
    {
        if(toastHeightOffset == 0)
            toastHeightOffset = (int) (activity.getResources().getDisplayMetrics().heightPixels * 0.2f);
        long now = SystemClock.uptimeMillis();
        if(now-lastToastTime > 1500)
        {
            if(activity == null)
                return;
            Toast t = Toast.makeText(AppContext.getContext(), msg, Toast.LENGTH_SHORT);
            View v = LayoutInflater.from(activity).inflate(R.layout.toast, null);
            t.setView(v);
            TextView tv = (TextView) v.findViewById(R.id.content);
            ImageView iv = (ImageView) v.findViewById(R.id.icon);
            t.setGravity(Gravity.BOTTOM, 0, toastHeightOffset);
            iv.setImageResource(iconId == -1 ? R.drawable.toast_info : iconId);
//        if(!TextUtils.isEmpty(msg))
//        {
//            if(msg.length() > 15)
//            {
//                tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 28);
//            }else
//            {
//                tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, 38);
//            }
            tv.setText(msg);
//        }
            t.show();
        }
        lastToastTime = now;
    }
}
